import numpy as np


def vectorize_label(y):
    """
    将标签label转化为与输出层相同大小的向量
    :param y: 标签值label
    :return: 标签对应的向量，神经网络的理想输出值
    """
    # 创建与输出层相同大小的零向量
    e = np.zeros((10, 1))
    # 将第y个值复值为1.0，作为该标签的输出理想值
    e[int(y)] = 1.0
    return e


def load_data(file, size):
    with open(file) as f:
        # 以,为分界，略去第一行，float数据形式完整读取数据集
        raw_data_list = np.loadtxt(f, dtype=float, delimiter=',', skiprows=1)
    # 数据集特征值维度和数据集个数
    len_pixel = raw_data_list.shape[1]
    len_data = raw_data_list.shape[0]
    # 暂时分离出数据集中x，和y
    temp_x = raw_data_list[:, 1:]
    temp_y = raw_data_list[:, 0]
    pixels = [np.reshape(x/255, (size[0], 1)) for x in temp_x]
    labels = [vectorize_label(y) for y in temp_y]
    data_list = list(zip(pixels, labels))
    return data_list


